//
//  EmotionPackage.swift
//  01-表情键盘 Emotion
//
//  Created by wulei on 15/12/15.
//  Copyright © 2015年 wulei. All rights reserved.
//

import UIKit

class EmotionPackage: NSObject {
// 表情包所在的路径
    var id: String?
    // 表情包名称,显示在toolbar 中
    var group_name_cn: String?
    // 表情数组,能够保证在使用的时候,数组已经存在,可以直接加载数据
    var emoticons = [Emoticon]()
    
    init(dict:[String:AnyObject]) {
        super.init()

       id = dict["id"] as? String
        group_name_cn = dict["group_name_cn"] as? String
        
        
        if let array = dict["emoticons"] as? [[String: AnyObject]] {
            
            var index = 0
            for var d in array {
                if let png = d["png"] as? String, dir = id {
                    
                    d["png"] = dir + "/" + png
                    
                    
                    
                }
                emoticons.append(Emoticon(dict: d))
                
                index++
                if index == 20{
                    emoticons.append(Emoticon(isRemoved: true))
                    index = 0
                }
                
            }
        }
        
        // 添加空白按钮
        appendEmptyEmoticon()
    }
    
// 在表情数组末尾添加空白按钮
    func appendEmptyEmoticon() {
//        print(emoticons.count % 21)
        let count = emoticons.count % 21
        if emoticons.count > 0 && count == 0 {
            return
        }
        
        for _ in count..<20 {
            emoticons.append(Emoticon(isEmpty: true))
        }
        
        // 尾末添加一个删除按钮
        emoticons.append(Emoticon(isRemoved: true))
    }
    
    
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    
    override var description: String {
    let key = ["id","group_name_cn","emoticons"]
        return dictionaryWithValuesForKeys(key).description
    
    }
    
}
